//
//  ZFMapManager+elevation.swift
//  map
//
//  Created by fyzq on 2024/2/2.
//

import Foundation
import MapboxMaps
///layer
private enum ZFElevationType  {
    static let elevation_line_source_id = "elevation_line_source_id"
    static let elevation_line_layer_id = "elevation_line_layer_id"
    static let elevation_text_layer_id = "elevation_text_layer_id"
}

extension ZFMapManager {
    
    ///添加等高线
    func setElevation() {
        guard let mapView = mapView,
            let mapboxMap = mapView.mapboxMap else {
            return
        }
        if(mapboxMap.layerExists(withId: ZFElevationType.elevation_line_layer_id)){
            return
        }
        
        var contourSource = VectorSource(id: ZFElevationType.elevation_line_source_id)
        contourSource.url = "mapbox://mapbox.mapbox-terrain-v2"
       
        var lineLayer = LineLayer(id: ZFElevationType.elevation_line_layer_id, source: ZFElevationType.elevation_line_source_id)
        lineLayer.sourceLayer = "contour"
        lineLayer.lineCap = .constant(.round)
        lineLayer.lineJoin = .constant(.round)
        
        let contourLineColor = UIColor(white: 1, alpha: 0.7)
        lineLayer.lineColor = .expression(Exp(.switchCase){
            Exp(.eq){
                Exp(.mod){
                    Exp(.get){"ele"}
                    50
                }
                0
            }
            contourLineColor
            Exp(.eq){
                Exp(.mod){
                    Exp(.subtract){
                        Exp(.get){"ele"}
                        5
                    }
                    25
                }
                0
            }
            contourLineColor
            UIColor.clear
        })

        var textLayer = SymbolLayer.init(id:ZFElevationType.elevation_text_layer_id, source: ZFElevationType.elevation_line_source_id)
        textLayer.sourceLayer = "contour"
        textLayer.symbolPlacement = .constant(.line)
        textLayer.textField = .expression(Exp(.switchCase){
            Exp(.eq){
                Exp(.mod){
                    Exp(.get){"ele"}
                    50
                }
                0
            }
            Exp(.concat){
                Exp(.get){"ele"}
                " m"
            }
            Exp(.eq){
                Exp(.mod){
                    Exp(.subtract){
                        Exp(.get){"ele"}
                        5
                    }
                    25
                }
                0
            }
            Exp(.concat){
                Exp(.subtract){
                    Exp(.get){"ele"}
                    5
                }
                " m"
            }
            ""
        })
        textLayer.textSize = .constant(12)
        textLayer.textColor = .constant(StyleColor.init(.darkText))
        textLayer.textHaloColor = .constant(StyleColor.init(.white))
        textLayer.textHaloWidth = .constant(2.0);
        
        do {
             try mapboxMap.addSource(contourSource)
             try mapboxMap.addLayer(lineLayer)
             try mapboxMap.addLayer(textLayer)
        } catch {
            print("Error : \(error.localizedDescription)")
        }
    }
    
    ///更新layer
    func updateElevationVisibility(value:Bool){
        guard let mapView = mapView,
            let mapboxMap = mapView.mapboxMap else {
            return
        }
        if mapboxMap.layerExists(withId: ZFElevationType.elevation_line_layer_id) {
            do {
                try mapboxMap.updateLayer(withId: ZFElevationType.elevation_line_layer_id, type: CircleLayer.self) { layer in
                    layer.visibility = .constant(value ? .visible : .none)
                }
                try mapboxMap.updateLayer(withId: ZFElevationType.elevation_text_layer_id, type: SymbolLayer.self) { layer in
                    layer.visibility = .constant(value ? .visible : .none)
                }
            } catch {
                print("Failed updateElevationVisibility = \(error)")
            }
        }
    }
    
}
